package net.tv90.bfmccore.battlepass;

import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

public class BattlepassPapiShow extends PlaceholderExpansion {
    @Override
    public String getIdentifier() {
        return "battlepass";
    }

    @Override
    public String getVersion() {
        return "1.0.0";
    }

    @Override
    public String getAuthor() {
        return "TV90";
    }


    /**
     * battlepass_level 通行证等级
     * battlepass_point 通行证点数
     * battlepass_upgrade 还需多少点数升级通行证
     * battlepass_query_{1-100} 是否拥有{}级通行证
     * battlepass_reward_{1-100} 是否领取过{}级通行证奖励
     */
    @Override
    public String onRequest(OfflinePlayer player, String params) {

        int level = 1;
        int point = 0;
        int upgrade = Battlepass.POINT_TO_UPGRADE;
        BattlepassPoint battlepassPoint = Battlepass.playerBattlepassData.get(player.getName());
        if (battlepassPoint != null) {
            level = battlepassPoint.getLevel();
            point = battlepassPoint.getPoint();
            upgrade = Battlepass.POINT_TO_UPGRADE * level - point;
        }

        if (params.startsWith("level")) {
            return String.valueOf(level);
        } else if (params.startsWith("point")) {
            return String.valueOf(point);
        } else if (params.startsWith("upgrade")) {
            return String.valueOf(upgrade);
        } else if (params.startsWith("query_")) {
            int queryLevel;
            try {
                queryLevel = Integer.parseInt(params.split("_")[1]);
            } catch (Exception e) {
                return null;
            }
            if (level >= queryLevel) {
                return "§a你的通行证已经达到" + queryLevel + "级";
            } else {
                return "§c你的通行证还未达到" + queryLevel + "级";
            }
        } else if (params.startsWith("reward_")) {
            int rewardLevel;
            try {
                rewardLevel = Integer.parseInt(params.split("_")[1]);
            } catch (Exception e) {
                return null;
            }
            Player p = Bukkit.getPlayerExact(player.getName());
            if (p.hasPermission("bfmc.bps.reward." + Battlepass.CURRENT_BATTLE_PASS_NAME + "." + rewardLevel)) {
                return "------------------------------------";
            } else {
                return "§a你还未领取过该等级的奖励，点击领取";
            }
        }
        return null;
    }
}
